package com.invengo.module_check.ui.fragment.book_listing;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentBookListingBinding;

import java.io.Serializable;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import me.goldze.mvvmhabit.base.BaseFragment;

import static com.invengo.module_check.data.entity.ActionArgs.ACTION_LISTING;
import static com.invengo.module_check.data.entity.BundleKey.KEY_ACTION;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;

public class BookListingFragment extends BaseFragment<FragmentBookListingBinding, BookListingViewModel> {
    ShelfBean shelfBeans;
    ActionArgs actionArgs;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_book_listing;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        actionArgs = (ActionArgs) getArguments().getSerializable(KEY_ACTION);
        shelfBeans = (ShelfBean) getArguments().getSerializable(BundleKey.KEY_SHELFS);
        viewModel.updateShelfBean(shelfBeans);
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.listingBookEvent.observe(this, bookBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS, (Serializable) bookBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_LISTING);
            bundle.putBoolean(KEY_OP_STATE, bookBeans != null);
            bundle.putString(BundleKey.KEY_CURRENT_SHELF, viewModel.currentShelfName.getValue());
            bundle.putString(BundleKey.KEY_TITLE, "上架");
            NavHostFragment.findNavController(this).navigate(ACTION_LISTING.getResultAction(), bundle);
        });
    }
}
